import 'package:flutter/material.dart';
import 'package:demo/cal.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  static const NKeys = [
    "C", "del", "%", "÷", //
    "7", "8", "9", "x", //,
    "4", "5", "6", "-", //
    "1", "2", "3", "+", //
    "?", "0", ".", "=", //
  ];

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: '仿小米计算器',
      home: new MyCalculator(),
    );
  }
}

class MyCalculator extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyCalculator> {

  Cal _cal = new Cal();
  // List HistoryNum = [];
  String HistoryNum = "";
  void signInput(String num) {

    _cal.addKey(num);

    List _RecentList = [
      [
        ['','11'],
        ['+','86'],
        ['=','100'],
      ]
    ];

    setState(() {
      _RecentList = _cal.RecentList;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Container(
        height: double.infinity,
        child: (
          new Column(
            mainAxisAlignment: MainAxisAlignment.start,  
            children: <Widget>[
              Expanded(
                child: InfoContainer()
              ),
              Container(
                height:400.0,
                child: InputPanel(),
              )
            ],
          )
        )
      )
    );
  }

  Widget InfoContainer(){
    return Container(
      color: Color(0xfff6f6f6),
      padding: const EdgeInsets.only(top: 30.0),
      child: SingleChildScrollView(
        reverse: true,
        padding: const EdgeInsets.all(20.0),
        child: Container(
          child: Column(
            children: <Widget>[
              NumPanel(),
              RecentNumPanel()
            ], 
          ),
        )
      ),
    );
  }

  Widget RecentNumPanel(){
    List<Widget> _lists = [];
    List<Widget> _childLists = [];

    for(int i = 0; i  < _cal.RecentList.length; i++){
      List key = _cal.RecentList[i];
      for(int j = 0; j  < key.length; j++){
        var childKey = key[j];
        if(j==key.length-2){
          _childLists.add(RecentTextLine(childKey[0],childKey[1],true,false));
        }else if(j==key.length-1){
          _childLists.add(RecentTextLine(childKey[0],childKey[1],false,true));
        }else{
          _childLists.add(RecentTextLine(childKey[0],childKey[1],false,false));
        }
      }
      
      _lists.add(RecentList(_childLists));
      _childLists=[];
    };

    return Column(
      children: _lists,
    );
  }

  Widget NumPanel(){
    List<Widget> _lists = [];
    List<Widget> _childLists = [];

    for(int i = 0; i  < _cal.HistoryList.length; i++){
      List key = _cal.HistoryList[i];
      for(int j = 0; j  < _cal.HistoryList[i].length; j++){
        var childKey = _cal.HistoryList[i][j];
        _childLists.add(TextLine(childKey[0],childKey[1]));
      }
      
      _lists.add(HistoryList(_childLists));
      _childLists=[];
    };

    return Column(
      children: _lists,
    );
  }

  Widget InputPanel(){
    return Container(
      child:
        ColumenPanel(),
    );
  }

  Widget HistoryList (childList) {
    return Container(
      padding: EdgeInsets.only(bottom:10.0,top:10.0),
      decoration: new BoxDecoration(
        border: new Border(
          bottom:BorderSide(width:0.6,color: Color(0xff999999)),
        ),
      ),
      child: Container(
        child: Align(
          alignment: Alignment.bottomRight,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: childList
          )
        )
      )
    );
  }
  Widget RecentList (childList) {
    return Container(
      padding: EdgeInsets.only(top:15.0),
      child: Container(
        child: Align(
          alignment: Alignment.bottomRight,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: childList
          )
        )
      )
    );
  }

  Widget RecentTextLine (String symbol,String num,bool recent,bool eq) {
    return Container(
      child:Text.rich(TextSpan(
        children: [
          TextSpan(
            text:'  $symbol  ',
            style: TextStyle(
              color: eq?Color(0xff909090):Color(0xff303030),
              fontFamily: 'PTSans',
              fontSize: recent?30.0:20.0,
              height:1.2,
            ) 
          ),
          TextSpan(
            text:'$num',
            style: TextStyle(
              color: eq?Color(0xff909090):Color(0xff303030),
              fontFamily: 'PTSans',
              fontSize: recent?30.0:20.0,
              height:1.2,
            ) 
          ),  
          ]
        )
      )  
    );
  }
  Widget TextLine (String symbol,String num,) {
    return Container(
      child:Text.rich(TextSpan(
        children: [
          TextSpan(
            text:'  $symbol  ',
            style: TextStyle(
              fontFamily: 'PTSans',
              fontSize: 20.0,
              height:1.2,
            ) 
          ),
          TextSpan(
            text:'$num',
            style: TextStyle(
              fontFamily: 'PTSans',
              fontSize: 20.0,
              height:1.2,
            ) 
          ),  
          ]
        )
      )  
    );
  }

  Widget ColumenPanel () {
    List<Widget> rows = [];
    
    List<Widget> btns = [];
    int flex = 1;
    for(int i = 0; i  < MyApp.NKeys.length; i++){
      String key = MyApp.NKeys[i];
      if (key.isEmpty) {
        flex++;
        continue;
      } else {
        Widget b = PanelButton(key, flex: flex);
        btns.add(b);
        flex = 1;
      }
      if ((i + 1) % 4 == 0) {
        rows.add(
          Expanded(
            child: Row(
              children: btns,
            )
          )
        );
        btns = [];
      }
    }
    return Column(
      children: rows
    );
  }

  Widget PanelButton (String num,{int flex = 1}) {
    return Expanded(
      flex: flex,
      child: Container(
        color: num == '='?Color(0xfffc9703):Color(0xfffffdfe),
          child: OutlineButton(
          color: Color(0xff303030),
          textColor: num == 'C'?Color(0xfffc9703):Color(0xff303030),
          highlightedBorderColor: Color(0xffdddddd),
          borderSide: BorderSide(
            color:Color(0xffdddddd),
            width: 0.6,
          ),
          onPressed: (){
            signInput(num);
          },
          child: Container(
            child: Center(
              child:
                TextContainer(num)
            )
          )
        ),
      )
    );
  }

  Widget TextContainer (String num){
    if(num == 'del'){
      return Image(
        image: AssetImage('images/del.png'),
        width: 100.0,
      );
    }else{
      return Text(
        '$num',
        style: TextStyle(
          fontFamily: 'PTSans',
          fontSize: 26.0,
          color: num == '='?Colors.white:null,
        )
      );
    }
  }
}

  // Widget HistoryList () {
  //   return Container(
  //     padding: EdgeInsets.only(bottom:10.0),
  //     decoration: new BoxDecoration(
  //       border: new Border(
  //         bottom:BorderSide(width:0.6,color: Color(0xff999999)),
  //       ),
  //     ),
  //     child: Container(
  //       child: Align(
  //         alignment: Alignment.bottomRight,
  //         child: Column(
  //           crossAxisAlignment: CrossAxisAlignment.end,
  //           children: <Widget>[
  //             TextLine('','11'),
  //             TextLine('+','89'),
  //             TextLine('=','100'),  
  //             TextLine('','$HistoryNum')
  //           ]
  //         )
  //       )
  //     )
  //   );
  // }